package org.veridiandynamics.client.view;

import java.awt.CardLayout;
import java.awt.Container;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import org.veridiandynamics.server.dao.HibernateDriver;
import org.veridiandynamics.server.domain.Appointment;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

/**
 * @author Talia
 * This class represents contents of a
 * tab the nurse can access that lists out the appointments for a particular day.
 *
 */
public class ViewDailyAppointmentsPanel extends AbstractPanelLayout
{
	private JButton back;
	private Container container;
	private CardLayout c1;
	private boolean counter = true;

	/**
	 * Constructor for panel.
	 * 
	 * @param c1 the card layout used
	 * @param container container for panel
	 */
	public ViewDailyAppointmentsPanel(final CardLayout c1, final Container container)
	{
		numCenterPanels = 1;
		this.c1 = c1;
		this.container = container;
		initialize();
		MainWindow.welcomeMessage.setText("View appointments for today");

		panelLeft.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
				new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));

		JLabel lblMedicalHistorySample = new JLabel("Daily Appointment sample:");
		panelLeft.add(lblMedicalHistorySample, "4, 8");

		TextArea textArea = new TextArea();
		panelLeft.add(textArea, "4, 12");
		HibernateDriver driver = new HibernateDriver();
		List<Appointment> appointmentList = driver.GetAll(new Appointment());
		for (Appointment a: appointmentList)
		{
			try {
				textArea.append("Appointment: " + "\n" + a.toString());
			}
			catch(NullPointerException e)
			{
				if (counter )
				{
					JOptionPane.showMessageDialog(null, "Warning: not all appointments have vaild start dates");
					counter = false;
				}
			}
		}
		JLabel lblDf = new JLabel(" ");
		panelLeft.add(lblDf, "8, 12");
		back = new JButton("Back");
		back.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent event) 
			{
				c1.first(container);
				MainWindow.welcomeMessage.setText("Welcome");
			}
		});
		panelLeft.add(back, "8, 14");
	}

}